package com.study.composite.general.safe;

import java.util.ArrayList;
import java.util.List;

public class CompositeComponent extends Component {

    private List<Component> components;

    public CompositeComponent(String name) {
        super(name);
        this.components = new ArrayList<>();
    }

    @Override
    protected String operation() {
        for (int i = 0; i < components.size(); i++) {
            System.out.println(components.get(i).operation());
        }
        return this.name;
    }

    public void addChild(Component component){
        components.add(component);
    }

    public void removeChild(Component component){
        components.remove(component);
    }

    public Component getChild(int index){
        return components.get(index);
    }
}
